package com.zzp.annotate;

import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Aspect
@AllArgsConstructor
public class SubmitLimitAspect {

    private final StringRedisTemplate redisTemplate;

    // AOP实现（使用Redis）
    @Around("@annotation(submitLimit)")
    public Object around(ProceedingJoinPoint joinPoint, SubmitLimit submitLimit) throws Throwable {
        String key = "SUBMIT_LOCK:" +submitLimit.key() + ":"+ "zzp";
        if (redisTemplate.hasKey(key)) {
            throw new RuntimeException("请勿重复提交");
        }
        redisTemplate.opsForValue().set(key, "use", submitLimit.lockTime(), TimeUnit.SECONDS);
        return joinPoint.proceed();
    }
}
